package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianjipms.entity.VehicleAccess;
import com.tianjipms.service.VehicleAccessService;
import com.tianjipms.utils.Result;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/vehicleAccess")
public class VehicleAccessController {
    @Resource
    private VehicleAccessService vehicleAccessService;

    //分页查询所有车辆信息
    @GetMapping("/listAll")
    public Result listAll(@RequestParam(defaultValue = "1") Long pageNum,
                          @RequestParam(defaultValue = "5") Long pageSize){
        IPage<VehicleAccess> page = new Page<>(pageNum, pageSize);
        vehicleAccessService.page(page);
        return Result.ok(page);
    }

    //分页车牌查询车辆信息
    @GetMapping("/listByLicenseNumber")
    public Result listByLicenseNumber(@RequestParam String licenseNumber,
                               @RequestParam(defaultValue = "1") Long pageNum,
                               @RequestParam(defaultValue = "5") Long pageSize){
        IPage<VehicleAccess> page = new Page<>(pageNum, pageSize);
        vehicleAccessService.findVehiclAccessListByLicenseNumberPage(page,licenseNumber);
        return Result.ok(page);
    }
    //分页车牌查询车辆信息
    @GetMapping("/listByTime")
    public Result listByTime(@RequestParam Date startTime,
                             @RequestParam Date endTime,
                             @RequestParam(defaultValue = "1") Long pageNum,
                             @RequestParam(defaultValue = "5") Long pageSize){
        IPage<VehicleAccess> page = new Page<>(pageNum, pageSize);
        vehicleAccessService.findVehiclAccessListByTimePage(page,startTime,endTime);
        return Result.ok(page);
    }
    @Scheduled(cron = "0 12 17 * * *")
    public Result backupData() {
        // 构造备份文件名，格式为 backup_年月.txt
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMM");
        String fileName = "vehicle_access_information_" + now.format(formatter) + ".txt";

        // 查询数据
        List<VehicleAccess> vehicleAccessList = vehicleAccessService.list(null);

        // 将数据保存到文件
        try (FileWriter writer = new FileWriter(fileName)) {
            for (VehicleAccess access : vehicleAccessList) {
                String line = access.toString(); // 假设 VehicleAccess 类已实现 toString 方法
                writer.write(line);
                writer.write(System.lineSeparator());
            }
            // 日志记录或其他操作...
            System.out.println("Data backup completed successfully: " + fileName);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        // 可选：清理数据库中的备份数据
        // vehicleAccessMapper.delete(null);
        return Result.ok();
    }
}
